<?php

namespace Lib;
use \Fuel\Core\Validation;

class Validate extends Validation {

	public static $fields = array();

	public function add_fields($fields = array ()) {
		self::$fields = $fields;
		foreach (self::$fields as $field => $options) {
			$validate = isset($options['validate']) ? $options['validate'] : array();
			$label = isset($options['label']) ? $options['label'] : '';
			$this->add_field($field, $label, $validate);
		}
	}

	public function get_errors($open_tag = '<p>', $close_tag = '</p>') {
		$errors = '';
		$error_fields = array();
		foreach ($this->error() as $field => $val) {
			$error_fields[] = $field;
		}
		foreach (self::$fields as $field => $options) {
			if (in_array($field, $error_fields)) {
				if (isset($options['messages'])) {
					if (is_array($options['messages'])) {
						foreach ($options['messages'] as $rule => $msg) {
							if ($this->error($field)->get_message() !== '') {
								$errors .= $open_tag . $this->error($field)->get_message($msg) . $close_tag;
							}
						}
					}
				}
				else {
					$errors .= $open_tag . $this->error($field) . $close_tag;
				}
			}
		}

		return $errors;
	}

}
